package com.chinatelecom.epush.index;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chinatelecom.epush.R;
import com.chinatelecom.epush.async.LoginAsyncTask;
import com.chinatelecom.epush.db.SharedPreferencesData;
import com.chinatelecom.epush.im.LoginActivity;

public class LoginAppActivity extends Activity{
	private SharedPreferencesData mData;
	private TextView tvTitle;

	private LinearLayout llLogin;// 登录布局
	private LinearLayout llLogining;// 验证提示

	private EditText etMobile;// 手机输入框
	private EditText etIdentify;// 验证码输入框

	private Button btnSend;// 发送手机号码
	private Button btnConfirm;//确认验证码登录

	private ImageView ivAnim;// 验证动画
	private AnimationDrawable adMessage;

	private Handler mHandler;

	private String strMobile;// 手机号码
	private String strIdentify;// 验证码

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		mHandler = new UpdateHandler();
		// 保存density值
		getDensity();
		// UI初始
		initView();
	}
	
	/**
	 * 获取Density
	 */
	private void getDensity() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		float density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
		//Environments.DENSITY = density;
	}

	private void initView() {
		// 将返回、查找按钮隐藏
		findViewById(R.id.btn_back).setVisibility(View.GONE);
		findViewById(R.id.btn_home).setVisibility(View.GONE);
		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvTitle.setText(R.string.login);

		llLogin = (LinearLayout) findViewById(R.id.ll_login);
		llLogining = (LinearLayout) findViewById(R.id.ll_logining);

		etMobile = (EditText) findViewById(R.id.et_mobile);
		etIdentify = (EditText) findViewById(R.id.et_identify);

		btnSend = (Button) findViewById(R.id.btn_send);
		btnConfirm = (Button) findViewById(R.id.btn_confirm);

		ivAnim = (ImageView) findViewById(R.id.iv_anim);	
		mData = new SharedPreferencesData(this);

		// 发送按钮
		btnSend.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				strMobile = etMobile.getText().toString().trim();
				if ("".equals(strMobile)) {
					Toast.makeText(LoginAppActivity.this,
							R.string.mobile_cannot_null, Toast.LENGTH_SHORT)
							.show();
				} else {
					/**
					new LoginAsyncTask(LoginAppActivity.this, mHandler,
							LoginAsyncTask.TYPE_SEND_MOBILE).execute(strMobile);
							**/
				}
			}
		});

		/// 确认按钮
		btnConfirm.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				strMobile = etMobile.getText().toString().trim();
				strIdentify = etIdentify.getText().toString().trim();
				if("".equals(strMobile)){
					Toast.makeText(LoginAppActivity.this,
							R.string.mobile_cannot_null, Toast.LENGTH_SHORT)
							.show();
					startHomePage();
					
				}else if ("".equals(strIdentify)) {
					Toast.makeText(LoginAppActivity.this,
							R.string.identify_cannot_null, Toast.LENGTH_SHORT)
							.show();
					startHomePage();
				}		
				else {
					/**
					new LoginAsyncTask(LoginAppActivity.this, mHandler,
							LoginAsyncTask.TYPE_SEND_IDENTIFY).execute(
							mData.getLoginMobile(), strIdentify);
							**/
					startHomePage();
				}
			}
		});

	}


	// 请求服务端判断是否内部成员
	@SuppressLint("HandlerLeak")
	private class UpdateHandler extends Handler {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case LoginAsyncTask.TYPE_SEND_MOBILE:
				if (msg.obj == null) {
					Toast.makeText(LoginAppActivity.this, R.string.error_tryagain,
							Toast.LENGTH_SHORT).show();
				} else {
					List<Object> list = (ArrayList<Object>) msg.obj;
					if ("1".equals(list.get(0))) {
						mData.setLoginMobile(strMobile);
						Toast.makeText(LoginAppActivity.this,
								R.string.check_identify_msg, Toast.LENGTH_SHORT)
								.show();
					} else {
						Toast.makeText(LoginAppActivity.this,
								R.string.moblie_error_retry, Toast.LENGTH_SHORT)
								.show();
					}
				}
				break;
			case LoginAsyncTask.TYPE_SEND_IDENTIFY:
				if (msg.obj == null) {
					Toast.makeText(LoginAppActivity.this, R.string.error_tryagain,
							Toast.LENGTH_SHORT).show();
				} else {
					List<Object> list = (ArrayList<Object>) msg.obj;
					if ("1".equals(list.get(0))) {
						if (list.get(2) != null && list.get(3) != null) {
							mData.setLoginSign((String) list.get(2));
						} 
						Toast.makeText(LoginAppActivity.this,
								R.string.success_welcome, Toast.LENGTH_SHORT)
								.show();
					
						startHomePage();
					} else {
						String error = list.get(1).toString();
						Toast.makeText(LoginAppActivity.this, error,
								Toast.LENGTH_SHORT).show();
					}

				}

				break;
			case LoginAsyncTask.TYPE_VALIDATE_IDENTIFY:
				if (msg.obj == null) {
					Toast.makeText(LoginAppActivity.this,
							R.string.faild_validate_no_net, Toast.LENGTH_SHORT)
							.show();
			
				} else {
					List<Object> list = (ArrayList<Object>) msg.obj;
					if ("1".equals(list.get(0))) {
					
						startHomePage();
					} else {
						llLogin.setVisibility(View.VISIBLE);
						llLogining.setVisibility(View.GONE);
						Toast.makeText(LoginAppActivity.this,
								R.string.out_date_re_validate,
								Toast.LENGTH_SHORT).show();
					}
				}
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		// 启动动画效果
		ivAnim.setBackgroundResource(R.anim.loading);
		adMessage = (AnimationDrawable) ivAnim.getBackground();
		adMessage.start();
	}

	/**
	 * 进入程序主界面
	 */
	private void startHomePage() {
		//Intent intent = new Intent(LoginAppActivity.this, PushDemoActivity.class);
		Intent intent = new Intent(LoginAppActivity.this, LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		LoginAppActivity.this.finish();
	}
}
